package controllers;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import models.Operator;
import models.WebHraDetail;
import play.Play;
import services.AuthorityService;
import services.DeviceService;
import services.HraDetailService;
import services.UploadToAppService;
import utils.BizConstants;
import vos.DeviceVo;
import vos.HraDetailSearchVo;
import vos.HraDetailView;
import vos.ReturnCode;
import vos.ReturnListVo;

/**
 * 数据上传控制类
 * 
 * @ClassName: UploadDataController 
 * @author: wangsm
 * @date: 2016年6月18日 下午3:43:00
 */
public class UploadDataController extends BaseController {
	/**
	 * 跳转数据上传页面
	 *
	 * @Title: uploaddata 
	 * @return: void
	 */
	public static void uploaddata(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取上传失败数据列表
	 *
	 * @Title: getUpload 
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param begindate
	 * @param enddate
	 * @param idcard
	 * @param username
	 * @return: void
	 */
	public static void getUpload(int page,int rows,String sort,String order, 
			String begintime,String endtime,String idcard,String username){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		HraDetailSearchVo vo=new HraDetailSearchVo(page,rows,sort,order,idcard,username,begintime,endtime);
		long total=HraDetailService.countNotUpload(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<HraDetailView> lists=HraDetailService.listNotUpload(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 上传
	 *
	 * @Title: upload 
	 * @return: void
	 */
	public static void upload(String did){
		if(StringUtils.isEmpty(did)){
			renderFailedJson(ReturnCode.ErrorCode,"选中记录不能为空");
		}
		//获取数据
		HraDetailView web=HraDetailService.getDetail(did);
		if(web==null||web.isupload==true){
			renderFailedJson(ReturnCode.ErrorCode,"记录不存在或已上传");
		}
		String pdfName=web.pdfname;
		if(StringUtils.isEmpty(pdfName)){
			render("errors/404.html");
		}
		String[] pdfString=pdfName.split("_");
		String dateString=pdfString[1];
		String filePath=BizConstants.filePath+web.deviceid+"/"+dateString+"/"+web.idcard+"/"+pdfName;
		File file=Play.getFile(filePath);
		if(!file.exists()){
			renderFailedJson(ReturnCode.ErrorCode,"文件不存在");
		}
		DeviceVo device=DeviceService.getViewById(web.deviceid);
		if(StringUtils.isEmpty(device.organization)){
			renderFailedJson(ReturnCode.ErrorCode,"机构编号不能为空");
		}
		//上传数据
		boolean isupload=UploadToAppService.uploadToApp(file, device.organization, web.idcard, web.pdfname,web.huiyuanziliao_jianchariqi+" "+web.huiyuanziliao_jianchashijian+":00");
		if(isupload){
			//上传成功后将记录设置为已上传
			HraDetailService.updateIsUpload(did);
			renderFailedJson(ReturnCode.SuccessCode,"上传成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"上传失败");
		}
	}
	/**
	 * 批量上传
	 *
	 * @Title: betchUpload 
	 * @param ids
	 * @return: void
	 */
	public static void betchUpload(String ids){
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"选中记录不能为空");
		}
		String[] id=ids.split(",");
		List<String> listid=new ArrayList<String>();
		Collections.addAll(listid, id);
		String notsuc="";
		for(String ss:listid){
			//获取数据
			HraDetailView web=HraDetailService.getDetail(ss);
			if(web==null||web.isupload==true){
				renderFailedJson(ReturnCode.ErrorCode,"记录不存在或已上传");
			}
			String pdfName=web.pdfname;
			if(StringUtils.isEmpty(pdfName)){
				renderFailedJson(ReturnCode.ErrorCode,"文件不存在");
			}
			String[] pdfString=pdfName.split("_");
			String dateString=pdfString[1];
			String filePath=BizConstants.filePath+web.deviceid+"/"+dateString+"/"+web.idcard+"/"+pdfName;
			File file=Play.getFile(filePath);
			if(!file.exists()){
				renderFailedJson(ReturnCode.ErrorCode,"文件不存在");
			}
			DeviceVo device=DeviceService.getViewById(web.deviceid);
			if(StringUtils.isEmpty(device.organization)){
				renderFailedJson(ReturnCode.ErrorCode,"机构编号不能为空");
			}
			//上传数据
			boolean isupload=UploadToAppService.uploadToApp(file, device.organization, web.idcard, web.pdfname,web.huiyuanziliao_jianchariqi+" "+web.huiyuanziliao_jianchashijian+":00");
			if(isupload){
				//上传成功后将记录设置为已上传
				HraDetailService.updateIsUpload(ss);
			}
			else{
				notsuc+=web.username+";";
			}
		}
		if(StringUtils.isNotEmpty(notsuc)){
			renderFailedJson(ReturnCode.ErrorCode,"上传失败"+notsuc);
		}
		renderFailedJson(ReturnCode.SuccessCode,"上传成功");
	}
}
